package com.second.app.excutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @Author GYZ
 * @Date 2024/1/24
 * <pre>
 *  定制 线程工厂
 * </pre>
 */
public class CustomThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName("定制池中的线程对象名称:" + Math.random());
        return null;
    }
}

//线程池会拒绝任务，和讲义上有冲突
class RunTask {
    public static void main(String[] args) {
        //CustomThreadFactory customThreadFactory = new CustomThreadFactory();
        //ExecutorService executorService = Executors.newCachedThreadPool(customThreadFactory);
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> System.out.println("我在运行" + System.currentTimeMillis() + " " + Thread.currentThread().getName()));
    }
}
